package com.skivingcloud.report.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.skivingcloud.admin.logutils.OperateLog;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.ReturnT;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.report.constants.ReportConstant;
import com.skivingcloud.report.entity.ReportScreenContent;
import com.skivingcloud.report.service.ReportScreenContentService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.skivingcloud.report.entity.ReportScreenTpl;
import com.skivingcloud.report.service.ReportScreenTplService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 大屏模板表 控制层。
 *
 * @author skivingcloud
 * @since 2024-11-07
 */
@RestController
@Tag(name = "大屏模板表接口")
@RequestMapping("/reportScreenTpl")
public class ReportScreenTplController {

    private static final Logger log = LoggerFactory.getLogger(ReportScreenTplController.class);

    @Resource
    private ReportScreenTplService reportScreenTplService;
    @Resource
    private ReportScreenContentService reportScreenContentService;

    /**
     * 添加大屏模板表。
     *
     * @param reportScreenTpl 大屏模板表
     * @return {@code {code:0,message:"",data:true}} 添加成功，{@code {code:500,message:"",data:false}} 添加失败
     */
    @OperateLog(module="大屏模板表", description = "新增大屏模板表")
    @PostMapping("/save")
    @Operation(summary="保存大屏模板表")
    @PreAuthorize("hasAuthority('reportScreenTpl:save')")
    public ReturnT<Boolean> save(@Validated @RequestBody @Parameter(description="大屏模板表")ReportScreenTpl reportScreenTpl) {
        try{
            reportScreenTpl.setId(UuidUtil.get32UUID());
            reportScreenTpl.setCreator(UserUtil.getCurrentUser().getId());
            reportScreenTpl.setCreateTime(LocalDateTime.now());
            reportScreenTpl.setTenantId(UserUtil.getCurrentUser().getTenantId());
            reportScreenTpl.setDelFlag(ReportConstant.COMMON_YES_OR_NO_NO);
            return ReturnT.ok(reportScreenTplService.save(reportScreenTpl));
        }catch(Exception e){
            log.error("新增大屏模板表失败", e);
            return ReturnT.error("新增大屏模板表失败");
        }
    }

    /**
     * 根据主键删除大屏模板表。
     *
     * @param id 主键
     * @return {@code {code:0,message:"",data:true}} 删除成功，{@code {code:500,message:"",data:false}} 删除失败
     */
    @OperateLog(module="大屏模板表", description = "删除大屏模板表")
    @PostMapping("/remove/{id}")
    @Operation(summary="根据主键删除大屏模板表")
    @PreAuthorize("hasAuthority('reportScreenTpl:remove')")
    public ReturnT<Boolean> remove(@PathVariable("id") @Parameter(description="大屏模板表主键")Serializable id) {
        try{
            return ReturnT.ok(reportScreenTplService.removeById(id));
        }catch(Exception e){
            log.error("删除大屏模板表失败", e);
            return ReturnT.error("删除大屏模板表失败");
        }
    }

    /**
     * 根据主键更新大屏模板表。
     *
     * @param reportScreenTpl 大屏模板表
     * @return {@code {code:0,message:"",data:true}} 更新成功，{@code {code:500,message:"",data:false}} 更新失败
     */
    @OperateLog(module="大屏模板表", description = "修改大屏模板表")
    @PostMapping("/update")
    @Operation(summary="根据主键更新大屏模板表")
    @PreAuthorize("hasAuthority('reportScreenTpl:update')")
    public ReturnT<Boolean> update(@Validated @RequestBody @Parameter(description="大屏模板表主键")ReportScreenTpl reportScreenTpl) {
        try{
            reportScreenTpl.setUpdater(UserUtil.getCurrentUser().getId());
            reportScreenTpl.setUpdateTime(LocalDateTime.now());
            return ReturnT.ok(reportScreenTplService.updateById(reportScreenTpl));
        }catch(Exception e){
            log.error("保存大屏模板表失败", e);
            return ReturnT.error("保存大屏模板表失败！");
        }
    }

    /**
     * 根据大屏模板表主键获取详细信息。
     *
     * @param id 大屏模板表主键
     * @return 大屏模板表详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary="根据主键获取大屏模板表")
    public ReturnT<ReportScreenTpl> getInfo(@PathVariable("id") Serializable id) {
        ReportScreenTpl tpl = reportScreenTplService.getById(id);
        List<ReportScreenContent> components = reportScreenContentService.list(
                QueryWrapper.create().and(ReportScreenContent::getTplId).eq(id)
                        .and(ReportScreenContent::getDelFlag).ne(ReportConstant.COMMON_YES_OR_NO_YES));
        tpl.setComponents(components);
        return ReturnT.ok(tpl);
    }

    /**
     * 分页查询大屏模板表。
     *
     * @param parameter 分页对象
     * @return 分页对象
     */
    @PostMapping("/page")
    @Operation(summary="分页查询大屏模板表")
    @PreAuthorize("hasAuthority('reportScreenTpl:page')")
    public ReturnT<Page<ReportScreenTpl>> page(@RequestBody @Parameter(description="分页信息")PageParameter<ReportScreenTpl> parameter) {
        parameter.getParam().setTenantId(UserUtil.getCurrentUser().getTenantId());
        Page<ReportScreenTpl> page = reportScreenTplService.queryByPage(parameter);
        return ReturnT.ok(page);
    }
}